package com.syf.kit;

import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.syf.core.kit.SyFrameKit;

@SyFrameKit("创建jackson json mapper的工厂类")
public class SyfJsonMapperFactory {

    private SyfJsonMapper mapper;

    /**
     * true : json序列化时，字段为null，则不进行序列化
     */
    private boolean ignoreNull = true;

    private boolean ignoreUnknownFields = true;

    public SyfJsonMapperFactory(boolean ignoreNull, boolean ignoreUnknownFields) {
        this.ignoreNull = ignoreNull;
        this.ignoreUnknownFields = ignoreUnknownFields;
    }

    /**
     * 给出默认配置的json data mapper
     */
    public SyfJsonMapper getUnifiedJsonMapper() {
        if (mapper == null) {
            mapper = new SyfJsonMapper(ignoreNull, ignoreUnknownFields);
        }

        return mapper;
    }

    /**
     * 给出自定义的序列化和反序列化特性配置的 json data mapper
     */
    public SyfJsonMapper getCustomizedJsonMapper(SerializationConfig serializationConfig, DeserializationConfig deserializationConfig) {
        return new SyfJsonMapper(serializationConfig, deserializationConfig);
    }

    /**
     * 给出自定义的ObjectMapper的 json data mapper
     */
    public SyfJsonMapper getCustomizedJsonMapper(ObjectMapper objectMapper) {
        return new SyfJsonMapper(objectMapper);
    }
}
